package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * A_ResultRecord类
 * 实现记录结果的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class A_ResultRecord implements AxdrType {

    private byte[] dataCode = null;
    private OAD oad = null;
    private RCSD rcsd = null;
    private SubChoiceRespData respData = null;

    /**
     * 默认构造函数
     */
    public A_ResultRecord() {
    }

    /**
     * 设置数据编码
     * 
     * @param dataCode 数据编码
     */
    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    /**
     * 设置所有参数
     * 
     * @param oad      对象属性描述符
     * @param rcsd     记录采集存储描述符
     * @param respData 响应数据
     */
    public void setAll(OAD oad, RCSD rcsd, SubChoiceRespData respData) {
        this.oad = oad;
        this.rcsd = rcsd;
        this.respData = respData;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;

        this.oad = new OAD();
        codeLength += this.oad.decode(input);

        this.rcsd = new RCSD();
        codeLength += this.rcsd.decode(input);

        this.respData = new SubChoiceRespData();
        this.respData.setLength(this.rcsd.size());
        try {
            codeLength += this.respData.decode(input);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        int codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                try {
                    output.write(this.dataCode[i]);
                } catch (java.io.IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            if (this.respData != null && this.rcsd != null && this.oad != null) {
                codeLength = 0;
                try {
                    codeLength += this.respData.encode(output);
                    codeLength += this.rcsd.encode(output);
                    codeLength += this.oad.encode(output);
                } catch (RuntimeException e) {
                    throw e;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else {
                throw new RuntimeException("respData || rcsd || oad is null");
            }
        }
        return codeLength;
    }

    /**
     * 编码并保存
     * 
     * @param encodingSizeGuess 编码大小预估
     */
    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error encoding A_ResultRecord", e);
        }
        this.dataCode = revOStream.getArray();
    }

    @Override
    public String toString() {
        if (this.oad != null && this.rcsd != null && this.respData != null) {
            return "sequence: {" + "oad: " + this.oad + ", rcsd: " + this.rcsd + ", respData: " + this.respData + "}";
        } else {
            return "respData || rcsd || oad is null";
        }
    }

    /**
     * 获取结果
     * 
     * @return 结果
     */
    public SubChoiceRespData getResult() {
        return this.respData;
    }

    /**
     * 获取记录
     * 
     * @return 记录序列
     */
    public SubSeqOfRecords getRecords() {
        if (this.respData != null && this.respData.getChoiceIndex() == SubChoiceRespDataChoice.RECORDS) {
            return this.respData.getRecords();
        }
        return null;
    }

    /**
     * 获取DAR
     * 
     * @return 数据访问结果
     */
    public AxdrEnum getDar() {
        if (this.respData != null && this.respData.getChoiceIndex() == SubChoiceRespDataChoice.DAR) {
            return this.respData.getDar();
        }
        return null;
    }

    /**
     * 获取选择索引
     * 
     * @return 选择索引
     */
    public SubChoiceRespDataChoice getChoiceIndex() {
        if (this.respData != null) {
            return this.respData.getChoiceIndex();
        }
        return null;
    }
}